package com.osfw.framework.view;

import com.osfw.framework.utils.spring.SpringUtils;
import com.osfw.framework.utils.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * FwFreeMarkerViewResolver 自定义freemarker视图解析器，动态自定义全局函数
 */
@Component
@Order(2)
public class FwFreeMarkerViewResolver implements CommandLineRunner {
    private static Logger log = LoggerFactory.getLogger(FwFreeMarkerViewResolver.class);


    @Value("${spring.freemarker.view_service_packages}")
    private String servicePackage;

    @Value("${spring.freemarker.view_convert_packages}")
    private String convertPackage;

    @Autowired
    private FreeMarkerViewResolver resolver;

    @Override
    public void run(String... args) throws Exception {
        // 增加自定义视图
        resolver.setViewClass(FwFreemarkerView.class);
        // 添加自定义解析器
        Map map = resolver.getAttributesMap();
        try{
            // 要扫描的Service包名
            if(StringUtils.isNotEmpty(servicePackage)){
                Arrays.stream(servicePackage.replaceAll(", ", ",").split(",")).forEach(x -> {
                    Reflections reflections = new Reflections(x);
                    // 目标注解类
                    Set<Class<?>> set = reflections.getTypesAnnotatedWith(Service.class);
                    if (set != null) {
                        set.stream().filter(clz -> StringUtils.isNotEmpty(clz.getAnnotation(Service.class).value())).forEach(clz -> {
                            String key = clz.getAnnotation(Service.class).value();
                            if(StringUtils.isNotEmpty(key)){
                                map.put(key, SpringUtils.getBean(clz));
                            }
                        });
                    }
                });
            }

            // 要扫描的Convert包名
            if(StringUtils.isNotEmpty(convertPackage)) {
                Arrays.stream(convertPackage.replaceAll(", ", ",").split(",")).forEach(x -> {
                    Reflections reflections = new Reflections(x);
                    // 目标注解类
                    Set<Class<?>> set = reflections.getTypesAnnotatedWith(Component.class);
                    if (set != null) {
                        set.stream().filter(clz -> StringUtils.isNotEmpty(clz.getAnnotation(Component.class).value())).forEach(clz -> {
                            String key = clz.getAnnotation(Component.class).value();
                            if(StringUtils.isNotEmpty(key)){
                                map.put(key, SpringUtils.getBean(clz));
                            }
                        });
                    }
                });
            }
        }catch (Exception e) {
            log.error("扫描目标注解类异常", e);
        }
    }

}
